#define _CRT_SECURE_NO_WARNINGS 1

namespace mystr
{
	class string
	{
	public:
		typedef char* iterator;
		string(const char* str="")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size + 1];
			strcpy(_str, str);
		}

		string(const string& s)
			:_str(nullptr)
			,_size(0)
			,_capacity(0)
		{
			string tmp(s._str);
			Swap(tmp);
		}

		void Swap(string& tmp)
		{
			std::swap(_str, tmp._str);
			std::swap(_size, tmp._size);
			std::swap(_capacity, tmp._capacity);
		}

		const char* c_str() const 
		{
			return _str;
		}

		string& operator=(string s)
		{
			Swap(s);
			return *this;
		}

		iterator begin() const
		{
			return _str;
		}

		iterator end() const
		{
			return _str + _size + 1;
		}

		void reserve(size_t n)
		{
			if (n <= _capacity)
				return;
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}

		void resize(size_t n, char ch = '\0')
		{
			if (n > _capacity)
			{
				reserve(n);
				size_t i = 0;
				for (i = _size; i < _capacity; i++)
				{
					_str[i] = ch;
				}
			}
			_str[n] = '\0';
			_size = n;
		}


		void clear()
		{
			_str[0]='\0';
			_size = 0;
		}

		bool empty() const
		{
			return _size == 0;
		}

		void push_back(char ch)
		{
			/*if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';*/
			insert(_size, ch);
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		string& operator+=(const char* s)
		{
			append(s);
			return *this;
		}

		string& operator+=(const string& s)
		{
			if (_str == s._str)
			{
				string tmp(s._str);
				append(tmp._str);
			}
			else
				append(s._str);
			return *this;
		}

		string& append(const char* s)
		{
			/*size_t len = strlen(s);
			if (_size + len >= _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str + _size, s);
			_size += len;*/
			insert(_size, s);
			return *this;
		}

		string& append(const string& str)
		{
			append(str._str);
			return *this;
		}

		string& append(size_t n, char ch)
		{
			size_t i = 0;
			for (i = 0; i < n; i++)
			{
				*this += ch;
			}
			return *this;
		}

		string& append(const char* s,size_t n)
		{
			char* str = new char[n + 1];
			strncpy(str, s, n);
			str[n] = '\0';
			append(str);
			delete[] str;
			return *this;
		}

		string& append(const string& str, size_t subpos, size_t sublen)
		{
			assert(subpos <= sublen && subpos <= _size);
			sublen = sublen >= _size ? _size : sublen;
			char* s = new char[sublen - subpos + 1];
			strncpy(s, str._str + subpos, sublen - subpos);
			s[sublen - subpos] = '\0';
			append(s);
			delete[] s;
			return *this;
		}

		size_t size() const
		{
			return _size;
		}

		size_t capacity() const
		{
			return _capacity;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size&& pos >= 0);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size&& pos >= 0);
			return _str[pos];
		}

		string& insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			size_t i = 0;
			for (i = _size + 1; i > pos; i--)
			{
				_str[i] = _str[i - 1];
			}
			_str[pos] = ch;
			_size++;
			return *this;
		}

		string& insert(size_t pos, const char* s)
		{
			assert(pos <= _size);
			size_t len = strlen(s);
			if (_size + len >= _capacity)
			{
				reserve(_size + len);
			}
			size_t i = 0;
			for (i = _size + len; i >= pos + len; i--)
			{
				_str[i] = _str[i - len];
			}
			strncpy(_str + pos, s, len);
			_size += len;
			return *this;
		}

		string& insert(size_t pos, const string& str)
		{
			insert(pos, str._str);
			return *this;
		}

		string& erase(size_t pos = 0, size_t len = npos)
		{
			assert(pos <= _size);
			if (pos + len > _size || len == npos)
			{
				_str[pos] = '\0';
				_size = pos - 1;
			}
			else
			{
				strcpy(_str + pos, _str + len + pos);
				_size -= len;
			}
			return *this;
		}

		size_t find(char ch, size_t pos = 0) const
		{
			size_t  i = pos;
			for (i = 0; i < size(); i++)
			{
				if (_str[i] == ch)
					return i;
			}
			return npos;
		}

		const size_t find(const char* s, size_t pos = 0) const
		{
			char* ptr = strstr(_str + pos, s);
			if (!ptr) 
				return npos;
			return ptr - _str;
		}

		string substr(size_t pos = 0, size_t len = npos) const
		{
			assert(pos < _size);
			string tmp;
			if (pos + len >= _size || len == npos)
			{
				tmp+=_str + pos;
			}
			else
			{
				size_t i = 0;
				for (i = pos; i < pos + len; i++)
				{
					tmp += _str[i];
				}
			}
			return tmp;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator>(const string& s) const
		{
			return strcmp(_str, s._str) > 0;
		}

		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}

		bool operator<(const string& s) const
		{
			return !((*this > s) || (*this == s));
		}

		bool operator<=(const string& s) const
		{
			return !(*this > s);
		}

		bool operator>=(const string& s) const
		{
			return ((*this > s) || (*this == s));
		}

		~string()
		{
			delete[] _str;
			_size = 0;
			_capacity = 0;
		}
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		const static size_t npos = -1;
	};
}

ostream& operator<<(ostream& out, const mystr::string& s)
{
	cout << s.c_str() << endl;
	return out;
}

istream& operator>>(istream& in, mystr::string& s)
{
	char ch;
	const size_t N = 64;
	char buff[N];
	size_t i = 0;
	ch = in.get();
	while (ch != '\n')
	{
		buff[i++] = ch;
		if (i == N - 1)
		{
			buff[N - 1] = '\0';
			s += buff;
			i = 0;
		}
		ch = in.get();
	}
	buff[i] = '\0';
	s += buff;
	return in;
}